<?php
define('IN_CORE', true);
if (!isset($_POST) || empty($_POST)){
	exit;
}
require_once './core/core.php';
require_once(APP_CORE.'/alipay/alipay.config.php');
require_once(APP_CORE."/alipay/alipay_notify.class.php");
//计算得出通知验证结果
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyNotify();
if(!$verify_result){
	//echo "fail";
	//exit;
}
//定义当前请求的系统常量
define('G_CHARSET', 'utf-8');
define('NOW_TIME',      $_SERVER['REQUEST_TIME']);
define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']);
define('IS_GET',        REQUEST_METHOD =='GET' ? true : false);
define('IS_POST',       REQUEST_METHOD =='POST' ? true : false);
define('IS_PUT',        REQUEST_METHOD =='PUT' ? true : false);
define('IS_DELETE',     REQUEST_METHOD =='DELETE' ? true : false);
define('IS_AJAX',       ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false);
file_put_contents('post.txt', $_POST['trade_status']);
file_put_contents('order.txt', $_POST['out_trade_no']);
file_put_contents('notify_type.txt', $_POST['notify_type']);
//充值成功返回 success
$where = array('code' => $_POST['out_trade_no']);
$addmoney_record = M('member_addmoney_record');
$orderinfo = $addmoney_record->where($where)->find();
if ($_POST['trade_status'] == 'TRADE_SUCCESS' && $_POST['notify_type'] == 'trade_status_sync'){
	if(!empty($orderinfo) && $addmoney_record->where($where)->setField('status','已付款')){
		M('member_account')->add(array(
		'uid' => $orderinfo['uid'],
		'type'=>1,'pay' => '账户',
		'content' => '支付宝充值',
		'money' => $orderinfo['money'],
		'time' => $orderinfo['time']));
		if(M('member')->where(array('uid' => $orderinfo['uid']))->setInc('money',$orderinfo['money'])){
			echo 'SUCCESS';
			exit;
		}
	}
}elseif ($_POST['trade_status'] == 'TRADE_FINISHED' && $_POST['notify_type'] == 'trade_status_sync'){
	if($orderinfo['status'] == '已付款'){
		echo 'SUCCESS';
		exit;
	}
}elseif ($_POST['trade_status'] == 'WAIT_BUYER_PAY' && $_POST['notify_type'] == 'trade_status_sync'){
	if($orderinfo['status'] == '已付款'){
		echo 'SUCCESS';
		exit;
	}
}